﻿/**
* 两点计算类
*/
class CommonFunction {

    public constructor() {

    }

    private static token: number = 0;
    public static get Token(): number {
        return this.token++;
    }
    /** 两点间距离 */
    public static GetDistance(p1: egret.Point, p2: egret.Point): number {
        return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
    }
    
    /** 两点与X周角度*/
    public static GetRotation(p1: egret.Point, p2: egret.Point): number {
        return Math.atan2(p1.y - p2.y, p1.x - p2.x) * (180 / Math.PI);
    }

    /** 两点间移动距离 遇到点不走 */
    public static GetSpeed(targetP2: egret.Point, currentP1: egret.Point, SpeedNum: number): egret.Point {
        var speed: egret.Point = new egret.Point();
        var hypotenuse: number = this.GetDistance(targetP2, currentP1);
        if (hypotenuse == 0) {
            speed.x = 0;
            speed.y = 0;
            return speed;
        }
        speed.x = SpeedNum * (targetP2.x - currentP1.x) / hypotenuse;
        speed.y = SpeedNum * (targetP2.y - currentP1.y) / hypotenuse;
        return speed;
    }
    /** 两点间移动距离 一直走 */
    public static GetSpeedAuto(targetP2: egret.Point, currentP1: egret.Point, SpeedNum: number): egret.Point {
        var speed: egret.Point = new egret.Point();
        var hypotenuse: number = this.GetDistance(targetP2, currentP1);
        speed.x = SpeedNum * (targetP2.x - currentP1.x) / hypotenuse;
        speed.y = SpeedNum * (targetP2.y - currentP1.y) / hypotenuse;
        return speed;
    }

    public static numPrecentage(cint: number, mint: number, countCop: number): number {
        var value: number = Math.floor(cint / mint * countCop);
        if (value > countCop) {
            value = countCop;
        }
        return value;
    }

}